package com.id.coinscleaner

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.id.coinscleaner.common.ViewBindingActivity
import com.id.coinscleaner.databinding.ActivityPurchaseBinding
import com.id.coinscleaner.utils.Events
import com.id.coinscleaner.utils.SubSku
import com.id.coinscleaner.utils.UserCanceledPayException
import com.id.coinscleaner.utils.pay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class PurchaseActivity : ViewBindingActivity<ActivityPurchaseBinding>() {
<<<<<<< HEAD

    val fragmentManager = supportFragmentManager
    var retainFragment = RetainFragment()

=======
    val fragmentManager = supportFragmentManager
    var retainFragment = RetainFragment()
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
    override fun onInit(savedInstanceState: Bundle?) {
        binding.weeklyBtn.setOnClickListener {
            purchaseWeekly()
        }
        binding.purchaseButton.setOnClickListener {
            purchaseWeekly()
        }
        binding.cancelButton.setOnClickListener {
            finish()
        }
    }

    private fun openFragment(fragment: Fragment, tag: String) {
        val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.retainFragment, fragment, tag)
        //添加到返回按键堆栈，这样就可以执行碎片的第二条周期，也就是不会每次replace的时候都去执行onAttach
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }

    fun purchaseWeekly() {
<<<<<<< HEAD
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                pay(SubSku.SkuYearly).catch { e ->
                    if (e !is UserCanceledPayException) {
                        Events.sendEvent(
                            "v_sub_fail_${SubSku.SkuYearly.skuId}",
=======
        //TODO "改变相关的SKUID"
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                pay(SubSku.Sku1Yearly).catch { e ->
                    if (e !is UserCanceledPayException) {
                        Events.sendEvent(
                            "v_sub_fail_${SubSku.Sku1Yearly.skuId}",
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                            "reason" to e.message
                        )
                        Toast.makeText(
                            this@PurchaseActivity,
                            "Network Error",
                            Toast.LENGTH_SHORT
                        ).show()
                    } else {
<<<<<<< HEAD
                        Events.sendEvent("canceled_paywall_${SubSku.SkuYearly.skuId}")
=======
                        Events.sendEvent("canceled_paywall_${SubSku.Sku1Yearly.skuId}")
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                        openFragment(retainFragment, "1")
                        findViewById<FrameLayout>(R.id.retainFragment).visibility = View.VISIBLE
                    }
                }.collectLatest {
<<<<<<< HEAD
                    Events.sendEvent("v_sub_successful_${SubSku.SkuYearly.skuId}")
=======
                    Events.sendEvent("v_sub_successful_${SubSku.Sku1Yearly.skuId}")
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                }
            }
        }
    }
}